'''
@Problem description:
Given a 32-bit signed integer, reverse digits of an integer.
Assume we are dealing with an environment which could only hold integers
within the 32-bit signed integer range.
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
给定一个32bit的有符号整形数据，翻转他。如果溢出就返回0

@For example:
Input: 123
Output:  321

Input: -123
Output: -321

Input: 120
Output: 21
'''


class Solution:
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        re = []
        t = abs(x)
        while (t > 0):
            re.append(int(t % 10))
            t = int(t / 10)
        y = 0
        for i in range(len(re)):
            y = y * 10
            y += re[i]
        if (x > 0):
            if (y > 0x7FFFFFFF):
                return 0
            else:
                return y
        else:
            y = -y
            if (y < -0x80000000):
                return 0
            else:
                return y
